<?php
error_reporting(0);
if (isset($_POST['submit'])){
	$server = "172.21.147.31";
	$connectionInfo = array( "Database"=>"ss2g6", "UID"=>"ss2g6", "PWD"=>"group6", 'ReturnDatesAsStrings'=> true );
	$conn = sqlsrv_connect( $server, $connectionInfo );
		if ($conn == false){
			die( print_r( sqlsrv_errors(), true));
		}
		$sql = $_POST['query'];
		$params = array();
		$options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
		$stmt = sqlsrv_query( $conn, $sql , $params);
        
        /*retrieve the number of rows affected for insert , delete and update*/
        $rows_affected = sqlsrv_rows_affected($stmt);
        
}
else{
	$stmt = false;
}
?>


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>CSC206</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <!-- Le styles -->
    <link href="css/bootstrap.css" rel="stylesheet">
    <style type="text/css">
      body {
        padding-top: 60px;
        padding-bottom: 40px;
      }
      .sidebar-nav {
        padding: 9px 0;
      }
    </style>
    <link href="../assets/css/bootstrap-responsive.css" rel="stylesheet">

    <!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

    <!-- Le fav and touch icons -->
    <link rel="shortcut icon" href="../assets/ico/favicon.ico">
    <link rel="apple-touch-icon-precomposed" sizes="114x114" href="../assets/ico/apple-touch-icon-114-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="72x72" href="../assets/ico/apple-touch-icon-72-precomposed.png">
    <link rel="apple-touch-icon-precomposed" href="../assets/ico/apple-touch-icon-57-precomposed.png">
  </head>

  <body>

   <div class="navbar navbar-fixed-top">
      <div class="navbar-inner">
        <div class="container-fluid">
          <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </a>
          <a class="brand" href="index.php">CSC206 LAB4</a>
          <div class="nav-collapse">
            <ul class="nav">
            </ul>
          </div><!--/.nav-collapse -->
        </div>
      </div>
    </div>

    <div class="container-fluid">
      <div class="row-fluid">
        <div class="span2">
                <? include 'sidebar.php' ?>
        </div><!--/span-->
        <div class="span9">
		
		<?
		if($stmt != false && sqlsrv_has_rows($stmt))
		{		
			$row_count = sqlsrv_num_rows($stmt);
			$colnames = sqlsrv_field_metadata($stmt);
		?>
		
		<table class="table table-striped table table-condensed">
		
		<thead>
			<tr>
				<?
					foreach ($colnames as &$colname){
					echo "<th>";
					echo $colname['Name'];
					echo "</th>";
					}
				?>
			</tr>	
			</thead>
		<tbody>
		<?
			$rows_affected = sqlsrv_rows_affected( $stmt);
			while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)){
		?>
			<tr>
				<?
					foreach ($colnames as &$colname){
					$cellname = $colname['Name'];
					echo "<td>";
					if ($colname['Type'] == 93){
						$temp_date = explode(" ", $row[$cellname]);
						echo $temp_date[0];
					}
					else{
						echo $row[$cellname];
					}
					echo "</td>";
					}
				?>
			</tr>		  
		<?
			}
		?>				
		</tbody>
		</table>
		<?
		}
		else{
        
            /*retrieve the number of rows affected for insert , delete and update*/
            if( $rows_affected === false) {
                 /*die( print_r( sqlsrv_errors(), true));*/
                 echo "Error, query aborted.";
            } elseif( $rows_affected == -1) {
                  echo "No information available.<br />";
            } else {
                  echo $rows_affected." row(s) were affected.<br />";
            }
			
		}
		?>

          <div class="row-fluid">
            <div class="span12">
              <h3>Enter your query:</h3>
                    <form method="post" action="custom.php" class="well form-inline">
                    <div class="control-group">
                        <div class="controls">
                            <textarea name="query" class="span10" id="textarea" rows="5" placeholder = 'SQL query'></textarea>
                        </div>
                    </div>
                    <p>
                    <p><input type="submit" name="submit" class="btn btn-primary" href="#" /></p>
                    </form>
                    
            </div><!--/span-->
          </div><!--/row-->
        </div><!--/span-->
      </div><!--/row-->

      <hr>

      <footer>
        <p>&copy; SS2 GROUP 6 </p>
      </footer>

    </div><!--/.fluid-container-->
  </body>
</html>